package com.bawei.park2302.adapter


import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.lib_base.utils.utils.entity.ParkBeanItem
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ItemParkBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class ParkAdapter : PagingDataAdapter<ParkBeanItem,ParkAdapter.ParkViewHolder>(diffUtil){
    companion object{
        val diffUtil = object : ItemCallback<ParkBeanItem>(){
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem == newItem
            }
        }
    }

    private var mOnItemClickListener : OnItemClickListener ? = null

    private var mOnItemLongClickListener : OnItemLongClickListener ? = null


    class ParkViewHolder(itemView: View) : ViewHolder(itemView){
        val binding = DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding?.bean=item
        holder.binding?.parkStatus?.apply {
            when(item.spaceStatus){
                "空闲"->{
                    setTextColor(Color.GREEN)
                    setBackgroundResource(R.drawable.shape_spare_status)
                }
                "已占用"->{
                    setTextColor(Color.BLUE)
                    setBackgroundResource(R.drawable.shape_occupied_status)
                }
                else->{
                    setTextColor(Color.DKGRAY)
                    setBackgroundResource(R.drawable.shape_maintain_status)
                }
            }
        }

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position,item)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemClick(item)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(inflate)
    }

    fun setOnItemChildClick(listener: OnItemClickListener?){
        this.mOnItemClickListener = listener
    }

    fun setOnItemChildLongClick(listener: OnItemLongClickListener?){
        this.mOnItemLongClickListener = listener
    }
}

interface OnItemClickListener{
    fun onItemClick(position: Int,bean : ParkBeanItem)
}

interface OnItemLongClickListener{
    fun onItemClick(bean: ParkBeanItem)
}